using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Linq;
using System.Data.Linq;


namespace RetailAnalytic.Tools.MDXTemplate
{
    public partial class UcFunc : UserControl
    {
        FrmFunctions _parF;
        public UcFunc(FrmFunctions parForm)
        {
            InitializeComponent();
            SetGuiAppearance();
            _parF = parForm;
            TemplateSqlClassDataContext dc = new TemplateSqlClassDataContext(RetailAnalytic.Core.SessionConfiguration.Dbconnection);
            var custOrders = from tg in dc.TemplateGroups
                             join t in dc.Templates
                             on tg.GroupId equals t.GroupId into os
                             select new { tg, os };
            foreach (var el in custOrders)
            {
                utFunc.Nodes.Add(el.tg.GroupId.ToString(), el.tg.Name);
                utFunc.Nodes[el.tg.GroupId.ToString()].LeftImages.Add(this.imageList1.Images[0]);
                utFunc.Nodes[el.tg.GroupId.ToString()].Tag = el.tg.Desc;
                foreach (var tm in el.os)
                {
                    List<string> ls = new List<string> { tm.TemplateId.ToString(), tm.Name, tm.Desc, tm.Formula, tm.GroupId.ToString(), tm.TemptypeId.ToString() };
                    utFunc.Nodes[tm.GroupId.ToString()].Nodes.Add("i: " + tm.TemplateId.ToString(), tm.Name);
                    utFunc.Nodes[tm.GroupId.ToString()].Nodes["i: " + tm.TemplateId.ToString()].LeftImages.Add(this.imageList1.Images[2]);
                    utFunc.Nodes[tm.GroupId.ToString()].Nodes["i: " + tm.TemplateId.ToString()].Tag = ls;
                }
            }

        }
        private void SetGuiAppearance()
        {
            this.imageList1.Images.Add("0",SR.ImagesLibrary.GetImageByName("close"));
            this.imageList1.Images.Add("0", SR.ImagesLibrary.GetImageByName("open"));
            this.imageList1.Images.Add("0", SR.ImagesLibrary.GetImageByName("f1"));
        }

        private void utFunc_AfterSelect(object sender, Infragistics.Win.UltraWinTree.SelectEventArgs e)
        {
            if (e.NewSelections[0].Level == 0)
            {
                _parF.rtbComment.Text = e.NewSelections[0].Tag.ToString();
                _parF.btNext.Enabled = false;
            }
            else if (e.NewSelections[0].Level == 1)
            {
                List<string> desc = (List<string>)e.NewSelections[0].Tag;
                _parF.rtbComment.Text = desc[2];
                _parF.queryMDX = desc[3];
                _parF.btNext.Enabled = true;
            }

        }

        private void utFunc_AfterExpand(object sender, Infragistics.Win.UltraWinTree.NodeEventArgs e)
        {
            if (e.TreeNode.Level == 0)
            {
                e.TreeNode.LeftImages.Clear();
                e.TreeNode.LeftImages.Add(this.imageList1.Images[1]);
            }
        }

        private void utFunc_AfterCollapse(object sender, Infragistics.Win.UltraWinTree.NodeEventArgs e)
        {
            if (e.TreeNode.Level == 0)
            {
                e.TreeNode.LeftImages.Clear();
                e.TreeNode.LeftImages.Add(this.imageList1.Images[0]);
            }
        }
    }
}
